using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Srvtools;

namespace cLimUse
{
    public partial class Form1 : InfoForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void infoDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        ////////////
        public string GetToday()
        {
            return string.Format("{0:yyyyMMdd}", DateTime.Today);
        }

        public string GetNowTime()
        {
            return string.Format("{0:hhmmss}", DateTime.Now);
        }

        public string GetUser()
        {
            return CliUtils.fLoginUser;
        }

        private void ibsMaster_CurrentChanged(object sender, EventArgs e)
        {
           string state = infoNavigator1.GetCurrentState();
           if (state == "Inserting" || state == "Editing" || state == "Initial" || state == "Browsed")
           {
               //修改時間顯示
               string UPDATE_TIME = ((DataRowView)ibsMaster.Current)["UPDATE_TIME"].ToString();
               if (UPDATE_TIME.Length == 6)
                infoTextBox5.Text = Convert.ToDateTime(UPDATE_TIME.Substring(0, 2) + ":" + UPDATE_TIME.Substring(2, 2) + ":" + UPDATE_TIME.Substring(4, 2)).ToLongTimeString(); 
               else if (UPDATE_TIME.Length == 0)
                   infoTextBox5.Text = "";


               //建檔時間顯示
               string CREATE_TIME = ((DataRowView)ibsMaster.Current)["CREATE_TIME"].ToString();
               if (CREATE_TIME.Length == 6)
                infoTextBox3.Text = Convert.ToDateTime(CREATE_TIME.Substring(0, 2) + ":" + CREATE_TIME.Substring(2, 2) + ":" + CREATE_TIME.Substring(4, 2)).ToLongTimeString(); 
               else if (CREATE_TIME.Length == 0)
                   infoTextBox3.Text = "";
           }
        }

        private void infoDataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if ((infoNavigator1.GetCurrentState() == "Inserting") || (infoNavigator1.GetCurrentState() == "Editing"))
            {
                float AM_AMT = 0, QT_QTY = 0, QT_QTY_TEMP;

                string Qty = infoDataGridView1.Columns[e.ColumnIndex].DataPropertyName.ToString();
                if (Qty == "QT_QTY")  
                {
                    //QT_QTY不能大於QT_QTY_TEMP
                    QT_QTY = float.Parse(((DataRowView)ibsDetail.Current)["QT_QTY"].ToString());
                    QT_QTY_TEMP = float.Parse(((DataRowView)ibsDetail.Current)["QT_QTY_TEMP"].ToString());
                    if (QT_QTY > QT_QTY_TEMP)
                    {
                        System.Windows.Forms.MessageBox.Show("領料數量超過庫存數量");
                        ((DataRowView)ibsDetail.Current)["QT_QTY"] = ((DataRowView)ibsDetail.Current)["QT_QTY_TEMP"].ToString();
                    }
                    //計算實售金額＝數量*售價 
                    AM_AMT = float.Parse(((DataRowView)ibsDetail.Current)["PR_COST"].ToString());
                    AM_AMT = AM_AMT * float.Parse(((DataRowView)ibsDetail.Current)["QT_QTY"].ToString());
                    ((DataRowView)ibsDetail.Current)["AM_COST"] = AM_AMT.ToString();
                    ibsDetail.EndEdit();
                   
                }
            }
        }

        private void bindingNavigatorEditItem_Click(object sender, EventArgs e)
        {
            //以string儲存,在按下修改時  
            ((DataRowView)ibsMaster.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsMaster.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsMaster.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now); 

        }

        private void ibsMaster_AddingNew(object sender, AddingNewEventArgs e)
        {                        
            //在新增就顯示建檔時間
            infoTextBox3.Text = DateTime.Now.ToLongTimeString();
        }

        private void InfoComboBox1_TextChanged(object sender, EventArgs e)
        {
                        
        }

        private void InfoRefValBox01_Leave_1(object sender, EventArgs e)
        {
          
            
        }

        private void InfoRefValBox02_Leave(object sender, EventArgs e)
        {
           //讓ColumnMatch所帶出的值,可以立刻顯示出RefVal的值
            ((DataRowView)ibsMaster.Current).EndEdit();
        }

        private void InfoComboBox0_TextChanged(object sender, EventArgs e)
        {
            //只有領料類別為存貨轉備品時,備品倉欄位才啟用                      
            if (InfoComboBox0.Text == "UC" || InfoComboBox0.Text == "存貨轉備品")
            {
                InfoRefValBox1.Enabled = true;
            }
            else
            {
                InfoRefValBox1.Enabled = false;
            }    
        }

        public string GetWH()
        {
            return InfoRefValBox01.TextBoxSelectedValue;
        }

        private void ibsMaster_AfterEdit(object sender, EventArgs e)
        {
            //以string儲存 
            ((DataRowView)ibsMaster.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsMaster.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsMaster.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now); 

        }

        private void infoDataGridView2_Click(object sender, EventArgs e)
        {
            //重新設定明細欄位的ReadOnly屬性(因為使用AutoDisableControl會造成回歸預設值False)
            this.infoDataGridView2.ReadOnly = true;
        }

        private void infoDataGridView1_Click(object sender, EventArgs e)
        {
            //重新設定明細欄位的ReadOnly屬性(因為使用AutoDisableControl會造成回歸預設值False)
            this.nOVOUDataGridViewTextBoxColumn1.ReadOnly = true;
            this.cNSEQDataGridViewTextBoxColumn.ReadOnly = true;
            this.nMITEMDataGridViewTextBoxColumn.ReadOnly = true;
            this.aMCOSTDataGridViewTextBoxColumn.ReadOnly = true;
            this.qTQTYENDDataGridViewTextBoxColumn.ReadOnly = true;
        }

        private void infoNavigator1_StateChanged(object sender, InfoNavigatorStateChangedEventArgs e)
        {
            //只有領料類別為存貨轉備品時,備品倉欄位才啟用                      
            if (InfoComboBox0.Text == "UC" || InfoComboBox0.Text == "存貨轉備品")
            {
                InfoRefValBox1.Enabled = true;
            }
            else
            {
                InfoRefValBox1.Enabled = false;            
            }
            
            //將DateTimePicker設為不可改
            this.InfoDateTimePicker01.Enabled = false;
            this.InfoDateTimePicker1.Enabled = false;

        }

        private void bindingNavigatorAddNewItem_MouseUp(object sender, MouseEventArgs e)
        {
            //只有領料類別為存貨轉備品時,備品倉欄位才啟用                      
            if (InfoComboBox0.Text == "UC" || InfoComboBox0.Text == "存貨轉備品")
            {
                InfoRefValBox1.Enabled = true;
            }
            else
            {
                InfoRefValBox1.Enabled = false;
            }

            //將DateTimePicker設為不可改
            this.InfoDateTimePicker01.Enabled = false;
            this.InfoDateTimePicker1.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //隱藏鍵值欄位          
            this.nOVOUDataGridViewTextBoxColumn1.Visible = false;
        }

        private void ibsDetail_BeforeEdit(object sender, EventArgs e)
        {
            //記錄Detail的修改時間日期
            ((DataRowView)ibsDetail.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsDetail.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsDetail.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);
        }
        public bool CheckDetail(object Value)
        {
            if (ibsDetail.List.Count == 0)
                return false;
            else
                return true;
        }

        private void rvWH_OnReturn(object sender, OnReturnEventArgs e)
        {
           //更換倉庫時,清除明細
            int n = ibsDetail.List.Count;
            for (int i = 0; i < n; i++)
                ibsDetail.List.RemoveAt(0);
        }

    }
}

